<?
include("../webkit/include/simple_db.php");
include("session.php");
$db = new SimpleDB();

$condition = "user_name='".$_SESSION['uid']."'";
if(isset($_GET['search_key'])) {
	$n = $_GET['search_key'];
	$condition .= " and (client_name like '%$n%' or order_no='$n' or 
	mobilephone='$n' or telephone1='$n' or telephone2='$n' or telephone3='$n')";
}
if(isset($_GET['order_status'])) {
	$condition .= " and order_status = ".$_GET['order_status'];
}

$page_size = 8;
$page = 1;
if(isset($_GET['page'])) {
	$page = intval($_GET['page']);
}
$start_index = ($page-1)*$page_size;

$sql = "select a.id,a.order_no, b.client_name, b.address, b.mobilephone, b.telephone1, a.problem_desc,
a.call_time, a.appt_time, a.order_status, a.accept_text, a.accept_time, a.hasten_count 
from jx_order a Left Join jx_client b ON a.client_id=b.id where $condition order by id desc limit $start_index, $page_size"; 
//echo $sql;
$db->query($sql);
?>
<!DOCTYPE html>
<head>
<meta http-equiv="Expires" CONTENT="0"/>
<meta http-equiv="Cache-Control" CONTENT="no-cache"/>
<meta http-equiv="Pragma" CONTENT="no-cache"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
</head>
<body>


<div data-role="header" data-theme="b"  data-position="fixed">
    <h1>96365工单列表</h1>
</div>

<!--
<a href="#" submit data-role="button" data-icon="star" data-theme="b"96365客服中心工单列表</a>
-->

<form>
     <input type="search" id="search_key" value="查工单编号、客户电话或名称" onfocus="this.value=''">
</form>

<div data-role="footer" class="ui-bar" data-theme="b">
	<a href="#" data-icon="search"  data-theme="e" onclick="do_search()">查询</a>
	<a href="#" data-icon="star"  data-theme="c" onclick="list_by_status(2)">待办</a>
	<a href="#" data-icon="check"  data-theme="c" onclick="list_by_status(3)">已回</a>
    <a href="#" data-icon="info"  data-theme="c" onclick="list_by_status(4)">改约</a>
</div>

<div data-role="collapsible-set" data-theme="c" data-content-theme="d" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d">
		<!--List Begin-->
		<?
		$i = 1;
		while($db->nextRecord()) {
			$rec = $db->getCurrentRecord();
			$order_status = array(1=>"待处理", 2=>"已派单", 3=>"回单", 4=>"改约");
			$status_tips = "";
			if($rec['order_status'] == 2) {
				$status_tips = "<font color='red'>新</font>";
			}
			if($rec['order_status'] == 4) {
				$status_tips = "<font color='#ff9900'>改</font>";
			}
			if($status_tips != '' && $rec['hasten_count'] > 0) {
				$status_tips .= "+<font color='green'>催</font>";
			}
			$tel = $rec['mobilephone'];
			if($rec['telephone1'] != '') {
				$tel .= "，".$rec['telephone1'];
			}
			$call_dt = split(" ", $rec['call_time']);
		?>
		<div data-role="collapsible">
			<h3><?echo $rec['client_name']?> <font color="gray">(<?echo $call_dt[0]?>)</font> <?echo $status_tips?></h3>
		
			<div data-role="fieldcontain">
				<label for="order_no_<?echo $i?>" style="color:#990000">工号编号：</label>
				<input type="text" id="order_no_<?echo $i?>" value="<?echo $rec['order_no']?>" readonly>

				<label for="telephone" style="color:#990000">联系电话：</label>
				<input type="text" id='telephone' value="<?echo $tel?>" readonly>

				<label for="telephone" style="color:#990000">客户地址：</label>
				<input type="text" value="<?echo $rec['address']?>" readonly>

				<label for="order_status" style="color:#990000">工单状态：</label>
				<input type="text" id="order_status" value="<?echo $order_status[$rec['order_status']]?>" readonly>

				<label for="call_time" style="color:#990000">受理时间：</label>
				<input type="text" id="call_time" value="<?echo $rec['call_time']?>" readonly>

				<label for="problem_desc" style="color:#990000">问题描述：</label>
				<textarea cols="40" rows="8" id="problem_desc" readonly><?echo $rec['problem_desc']?></textarea>

				<label for="appt_time" style="color:#990000">预约时间：</label>
				<input type="text" id="appt_time" value="<?echo $rec['appt_time']?>" readonly>

				<label for="hasten_count" style="color:#990000">催单次数：</label>
				<input type="text" id="hasten_count" value="<?echo $rec['hasten_count']?>" readonly>

				<label for="accept_text_<?echo $i?>" style="color:#0066cc">回单备注：</label>
				<textarea cols="40" rows="8" id="accept_text_<?echo $i?>" <?if($rec['order_status']==3){echo "readonly";}?>><?echo $rec['accept_text']?></textarea>

				
				<label for="accept_time_<?echo $i?>" style="color:#990000">更新时间：</label>
				<input type="text" id="accept_time_<?echo $i?>" value="<?echo $rec['accept_time']?>" readonly>
			</div>

			<? if($rec['order_status'] !=3 ) { // 如果已经回单，则不再显示此按钮?>
			<a href="#" data-role="button" data-theme="e" data-inline="true" data-icon="edit" id="btn_accept_<?echo $i?>" onclick="do_confirm(3, <?echo $i?>); return false">回单</a>　<span style='color:red;display:none' id="op_result_tips_<?echo $i?>">数据已成功保存!</span>
			<? } ?>
		</div>
		<?
			$i++;
		}?>
		<!--List End-->
</div>

<div data-role="footer" class="ui-bar" data-theme="b" data-position="fixed">
	<a href="#" data-icon="bars" data-theme="e" onclick="window.location = base_url+'list.php'">重新加载</a>
    <a href="#" data-icon="arrow-l" onclick="prev_page(<?echo $page?>)">上一页</a>
    <a href="#" data-icon="arrow-r" onclick="next_page(<?echo $page?>)">下一页</a>
</div>

<script>
	var base_url = "<?echo $base_url?>/mobile/";
	function prev_page(p) {
		if(p > 1) {
			window.location = base_url+"list.php?page="+(p-1);
		}
	}

	function next_page(p) {
		window.location = base_url+"list.php?page="+(p+1);
	}

	function do_search() {
		var v = $("#search_key").val()
		if(v != '') {
			//showLoader();
			window.location = base_url+'list.php?search_key='+v;
		}
	}

	function list_by_status(n) {
		window.location = base_url+'list.php?order_status='+n;
	}


	var is_posting = false; // 正在提交数据标志;  added @2013/9/22

	function do_confirm(result, index) {
		if($('#accept_text_'+index).val() == "") {
			$("#op_result_tips_"+index).html('回单备注不能为空!');
			$("#op_result_tips_"+index).show('slow');
			setTimeout("hideTips("+index+")", 2000);  // 2秒后隐藏掉操作结果tips
			return false;
		}

		if( is_posting ) {
			return false;	
		}

		//showLoader();
		var params = {'order_no':$('#order_no_'+index).val(), 'order_status':result, 
			'accept_text':$('#accept_text_'+index).val()};

		is_posting = true; // 设置提交中标志
		$("#op_result_tips_"+index).html('正在提交中...');
		$("#op_result_tips_"+index).show('slow');

		$.getJSON(
			base_url+"query.php?callback=?&action=do_confirm", params, 
			function(data) {
				//alert(data.accept_time)
				//hideLoader();
				is_posting = false;
				$("#accept_time_"+index).val(data.accept_time);  // 显示回单操作时间
				$("#btn_accept_"+index).css('display', 'none');  // 隐藏掉"回单"按钮
				$("#op_result_tips_"+index).html('数据已成功保存!');
				$("#op_result_tips_"+index).show('slow');
				setTimeout("hideTips("+index+")", 2000);  // 2秒后隐藏掉操作结果tips
			}
		);
		
	}

	function hideTips(index) {
		$("#op_result_tips_"+index).hide('slow');
	}

	function showLoader() {  
		//显示加载器.for jQuery Mobile 1.2.0  
		$.mobile.loading('show', {  
			text: '数据保存中...', //加载器中显示的文字  
			textVisible: true, //是否显示文字  
			theme: 'a',        //加载器主题样式a-e  
			textonly: false,   //是否只显示文字  
			html: ""           //要显示的html内容，如图片等  
		});  
	}  
	  
	//隐藏加载器.for jQuery Mobile 1.2.0  
	function hideLoader()  
	{  
		//隐藏加载器  
		$.mobile.loading('hide');  
	}  
</script>

</body>
</html>